import random
import mysql.connector
from faker import Faker
from common import snowFaker as sf
from python_faker.faker03 import generate_dish_name
from python_faker.faker04 import getData

# 初始化Faker实例
fake = Faker('zh_CN')

# 连接到数据库
conn = mysql.connector.connect(
    host='localhost',
    user='root',
    password='123456',
    database='esl'
)

cursor = conn.cursor()

# 假设你已经准备好了菜品名称列表
# dishName = [
#     '宫保鸡丁', '水煮鱼', '红烧肉', '麻辣小龙虾', '蚝油生菜',
#     '鱼香茄子', '炸酱面', '蒜蓉小龙虾', '酸菜鱼', '铁板烧',
#     '北京烤鸭', '香煎鳕鱼', '青椒土豆丝', '东坡肉', '狮子头',
#     '干锅鸡', '香辣虾', '蒸饺', '蒜香排骨', '煎带鱼'
# ]

# menuId = 'd232590df50811ef8431489ebd74c6f4'
#
# canteenId = '52f94d0fea0e4244af14536b1fe42436'


# 生成虚拟菜品数据
def generate_fake_dish(dishName,menuId,canteenId):
    id = sf.get_id()
    dish_id = fake.uuid4().replace('-', '')
    name = dishName
    description = f"这道菜是{name}，口味独特，非常美味。"  # 菜品描述
    price = round(random.uniform(10, 100), 2)  # 随机生成价格
    avatar = 'img.png'  # 默认图片
    rating = round(random.uniform(3.0, 5.0), 1)  # 随机生成评分，范围3.0到5.0
    menu_id = menuId
    tag_id = random.choice(['15','16','17','18','19'])  # 随机标签ID
    canteen_id = canteenId

    # 假设使用当前时间作为创建时间
    create_time = fake.date_this_decade()

    return (id,dish_id, name, description, price, avatar, rating, menu_id, tag_id, canteen_id)


# 批量插入虚拟菜品数据
def insert_fake_dishes(dishName,menuId,canteenId):
    # 创建一个批量数据的容器
        data = generate_fake_dish(dishName, menuId, canteenId)
        # 使用executemany进行批量插入
        query = """INSERT INTO dish 
                   (id,dish_id, name, description, price, avatar, rating, menu_id, tag_id, canteen_id) 
                   VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
        cursor.execute(query, data)
        conn.commit()  # 提交事务

rows = getData()
for row in rows:
    for i in range(100):
        dishName = generate_dish_name(row[2])
        insert_fake_dishes(dishName,row[0],row[1])


# 关闭连接
cursor.close()
conn.close()

print("虚拟菜品数据已全部成功生成并插入数据库！")
